package com.huawei123.hasee.cniao5store.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.utils.LogUtils;
import com.huawei123.hasee.cniao5store.utils.UIUtils;

/**
 * 项目名称：Cniao5Store
 * 类描述：抽取公共的FrameLayout作为自定义控件
 * 创建人：Administrator
 * 创建时间：2017/10/26 0026 下午 6:59
 * 修改人：Administrator
 * 修改时间：2017/10/26 0026 下午 6:59
 * 修改备注：
 */

public abstract class LoadingPager extends FrameLayout {


    public LoadingPager(@NonNull Context context) {
        super(context);
        init();
    }

    public LoadingPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadingPager(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 默认状态
     */
    public static final int STATE_UNKOWN = 0;
    /**
     * 加载中
     */
    public static final int STATE_LOADING = 1;
    /**
     * 加载失败
     */
    public static final int STATE_ERROR = 2;
    /**
     * 空
     */
    public static final int STATE_EMPTY = 3;
    /**
     * 加载成功状态
     */
    public static final int STATE_SUCCESS = 4;

    /**
     * 当前状态
     */
    protected int state = STATE_UNKOWN;

    private View loadingView;//加载中界面
    private View errorView;//加载失败界面
    private View emptyView;//空界面
    private View successView;//加载成功界面


   /**
     * 初始化状态布局
     */
    private void init() {
        if (loadingView == null) {
            loadingView = createLoadingView();
            this.addView(loadingView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
        if (errorView == null) {
            errorView = createErrorView();
            this.addView(errorView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
        if (emptyView == null) {
            emptyView = createEmptyView();
            this.addView(emptyView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
        showPage();
    }

    /**
     * 根据状态显示界面
     */
    private void showPage() {
        if (loadingView != null) {
            loadingView.setVisibility(state == STATE_UNKOWN || state == STATE_LOADING ? View.VISIBLE : View.GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.GONE);
        }
        /*成功直接加载成功界面*/
        if (successView == null && state == STATE_SUCCESS) {
            successView = createSuccessView();
            this.addView(successView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
    }

    /**
     * 判定当前状态请求数据
     */
    public void show() {
        if (state == STATE_UNKOWN || state == STATE_ERROR || state == STATE_EMPTY) {
            state = STATE_LOADING;
            loading();
        }
        showPage();
    }

    /**
     * 在状态改变后要在需要改变界面，必须在UI线程执行
     *
     * @param state 界面状态
     */
    public void setState(LoadResult state) {
        this.state = state.getState();
        UIUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showPage();
            }
        });
    }

    /**
     * 界面状态枚举
     */
    public enum LoadResult {
        error(STATE_ERROR), success(STATE_SUCCESS), empty(STATE_EMPTY);
        private int state;

        LoadResult(int state) {
            this.state = state;
        }

        public int getState() {
            return state;
        }
    }

    /**
     * 加载界面
     *
     * @return
     */
    private View createLoadingView() {
        return UIUtils.inflate(R.layout.loading_page);
    }

    /**
     * 错误界面
     *
     * @return
     */
    private View createErrorView() {
        View view = UIUtils.inflate(R.layout.loading_error_page);
        Button settingBtn = (Button) view.findViewById(R.id.setting);
        settingBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入手机中的设置界面
                LogUtils.showToast("网络错误");
            }
        });
        //点击刷新界面
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });
        return view;
    }

    /**
     * 空页面
     *
     * @return
     */
    private View createEmptyView() {
        return UIUtils.inflate(R.layout.loading_page_empty);
    }

    /**
     * 需要子类重写的请求数据方法
     */
    protected abstract void loading();

    /**
     * 需要子类重写的加载成功界面
     *
     * @return
     */
    protected abstract View createSuccessView();

}
